package com.hddz.comlib.util;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ConvertUtility {
	/**
	 * @说明: 地理上1度对应实际米数
	 * @修改人: 戴卫国
	 * @修改日期: 2011-9-23 下午1:46:28
	 */
	public static final int DD = 111000;

	public static int BCDToInt(int bcd) {
		return bcd / 16 * 10 + bcd % 16;
	}

	public static String bystesToStr(byte[] s, int start, int len) {
		StringBuilder sb = new StringBuilder();
		for (int i = start; i < start + len; i++) {
			sb.append(String.format("%X ", s[i] & 0xff));
			sb.append(" ");
		}
		return sb.toString();
	}

	public static String bystesToStr(List<Byte> s, int start, int len) {
		StringBuilder sb = new StringBuilder();
		for (int i = start; i < start + len; i++) {
			sb.append(String.format("X2", s.get(i)));
			sb.append(" ");
		}
		return sb.toString();
	}

	public static Boolean checkByteInList(int b, List<Byte> bs, int start, int len) {
		for (int i = start; i < start + len; i++) {
			if (bs.get(i) == b) {
				return true;
			}
		}
		return false;
	}

	public static String byteBufferToString(ByteBuffer buff, int len) {
		CharBuffer charBuffer = null;
		String result = "";
		int perlimit = buff.limit();
		try {
			Charset charset = Charset.defaultCharset();
			CharsetDecoder decoder = charset.newDecoder();
			buff.limit(buff.position() + len);
			charBuffer = decoder.decode(buff);
			result = charBuffer.toString();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			buff.limit(perlimit);
		}
		return result;
	}

	public static Boolean checkByteInByteBuffer(int b, ByteBuffer buff, int len) {
		int perpostion = buff.position();
		Boolean result = false;
		try {
			while (buff.position() < perpostion + len) {
				if (buff.get() == b) {
					result = true;
					break;
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			buff.position(perpostion);
		}
		return result;
	}

	public static String byteListToString(List<Byte> bs, int start, int len) {
		if (bs == null) {
			return "";
		}
		try {
			ByteArrayOutputStream bout = new ByteArrayOutputStream(len);
			for (int i = 0; i < len; i++) {
				bout.write(bs.get(i + start));
			}
			return bout.toString("UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			return "";
		}
	}

	public static Boolean checkByteInList(byte b, byte[] bs, int start, int len) {
		for (int i = start; i < start + len; i++) {
			if (bs[i] == b) {
				return true;
			}
		}
		return false;
	}

	public static String findIntStr(String str) {
		String result = "";
		try {
			result = Integer.parseInt(str) + "";
		} catch (Exception e) {
			// TODO: handle exception
		}
		return result;
	}

	public static Date convertDateTime(String s) {
		try {
			return new SimpleDateFormat("yy/MM/dd HH:mm").parse(s);
		} catch (Exception e) {
			return null;
		}
	}

	public static String convertDateTime(Date d) {
		try {
			return new SimpleDateFormat("yyyy/MM/dd").format(d);
		} catch (Exception e) {
			return null;
		}
	}

	public static Date convertDateTime(String s, String format) {
		try {
			return new SimpleDateFormat(format).parse(s);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 
	 * @param b
	 *            byte[]
	 * @return String
	 */
	public static String Bytes2HexString(byte[] b) {
		String ret = "";
		for (int i = 0; i < b.length; i++) {
			String hex = Integer.toHexString(b[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			ret += hex.toUpperCase() + " ";
		}
		return ret;
	}

	/**
	 * @param b
	 * @param size
	 * @return
	 */
	public static String Bytes2HexString(byte[] b, int size) {
		String ret = "";
		for (int i = 0; i < size; i++) {
			String hex = Integer.toHexString(b[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			ret += hex.toUpperCase() + " ";
		}
		return ret;
	}

	/**
	 * 将两个ASCII字符合成一个字节； 如："EF"--> 0xEF
	 * 
	 * @param src0
	 *            byte
	 * @param src1
	 *            byte
	 * @return byte
	 */
	public static byte uniteBytes(byte src0, byte src1) {
		byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 })).byteValue();
		_b0 = (byte) (_b0 << 4);
		byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 })).byteValue();
		byte ret = (byte) (_b0 ^ _b1);
		return ret;
	}

	/**
	 * 将指定字符串src，以每两个字符分割转换为16进制形式 如："2B44EFD9" --> byte[]{0x2B, 0x44, 0xEF,
	 * 0xD9}
	 * 
	 * @param src
	 *            String
	 * @return byte[]
	 */
	public static byte[] HexString2Bytes(String src) {
		byte[] ret = new byte[src.length() / 2];
		byte[] tmp = src.getBytes();
		for (int i = 0; i < src.length() / 2; i++) {
			ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
		}
		return ret;
	}

	public static long convertToLong(String str) {
		long rel = 0;
		try {
			rel = Long.parseLong(str);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return rel;
	}

	/**
	 * 时间戳转换成日期格式字符串
	 * 
	 * @param seconds
	 *            精确到秒的字符串
	 * @param formatStr
	 * @return
	 */
	public static String timeStamp2Date(long miseconds, String format) {
		if (format == null || format.isEmpty())
			format = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date(miseconds));
	}

	/**
	 * 日期格式字符串转换成时间戳
	 * 
	 * @param date
	 *            字符串日期
	 * @param format
	 *            如：yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String date2TimeStamp(String date_str, String format) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return String.valueOf(sdf.parse(date_str).getTime() / 1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
}
